package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.GoodsCouponEntity;
import com.czy.owner.utils.TimeUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangyang on 2017-06-30
 */

public class PickGoodsCouponAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<GoodsCouponEntity> couponData = new ArrayList<>();
    private GoodsCouponEntity pickedGoodsCoupon ;
    private double sumGoodsPrice;
    private int cashColor = Color.parseColor("#FEA454"), disenbledColor = Color.parseColor("#EBEBEB");
    private int yellowColor = Color.parseColor("#FDCD54");
    private int themeColor = Color.parseColor("#09c0c7");
    public PickGoodsCouponAdapter(Context mContext, List<GoodsCouponEntity> couponData, GoodsCouponEntity pickedGoodsCoupon , double sumGoodsPrice) {
        this.mContext = mContext;
        this.couponData = couponData;
        this.pickedGoodsCoupon = pickedGoodsCoupon;
        this.sumGoodsPrice = sumGoodsPrice;
    }


    @Override
    public int getChildrenCount(int groupPosition) {
        return couponData.size();
    }

    @Override
    public int getGroupCount() {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public GoodsCouponEntity getChild(int groupPosition, int childPosition) {
        return couponData.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        return new View(mContext);
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder childViewHolder;
        if (convertView == null) {
            childViewHolder = new ChildViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pick_coupon, parent, false);
            childViewHolder.ll_bg = (LinearLayout) convertView.findViewById(R.id.ll_bg);
            childViewHolder.ll_xuxian = (LinearLayout) convertView.findViewById(R.id.ll_xuxian);
            childViewHolder.tv_use_store = (TextView) convertView.findViewById(R.id.tv_use_store);
            childViewHolder.cbPicked = (CheckBox) convertView.findViewById(R.id.cb_item_picked);
            childViewHolder.tvName = (TextView) convertView.findViewById(R.id.tv_item_coupon_name);
            childViewHolder.imgCouponsType = (ImageView) convertView.findViewById(R.id.img_coupons_type);
            childViewHolder.tvUsedCase = (TextView) convertView.findViewById(R.id.tv_used_case);
            childViewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_item_time);
            childViewHolder.tvUsedData = (TextView) convertView.findViewById(R.id.tv_used_data);
            childViewHolder.tvUsedMoney = (TextView) convertView.findViewById(R.id.tv_used_money);
            childViewHolder.tvUsedYuan = (TextView) convertView.findViewById(R.id.tv_used_yuan);
            childViewHolder.tvUsedDescription = (TextView) convertView.findViewById(R.id.tv_used_description);
            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        if (couponData.get(childPosition).getMinimum() > sumGoodsPrice) {
//            childViewHolder.cbPicked.setEnabled(false);
//            childViewHolder.cbPicked.setVisibility(View.GONE);
        } else {
            childViewHolder.cbPicked.setEnabled(true);
//            childViewHolder.cbPicked.setVisibility(View.VISIBLE);
        }
        childViewHolder.cbPicked.setTag(groupPosition + "," + childPosition);
        if(pickedGoodsCoupon !=null && pickedGoodsCoupon.getCouponsId() == couponData.get(childPosition).getCouponsId() && pickedGoodsCoupon.getCouponsGroupId() == couponData.get(childPosition).getCouponsGroupId() ){
            childViewHolder.cbPicked.setChecked(true);
        }else{
            childViewHolder.cbPicked.setChecked(false);
        }


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(couponData.get(childPosition).getMinimum() > sumGoodsPrice){
                    return;
                }
                childViewHolder.cbPicked.performClick();
            }
        });



        childViewHolder.cbPicked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox) v).isChecked()){
                    pickedGoodsCoupon = couponData.get(childPosition);
                }else{
                    pickedGoodsCoupon = null;
                }
                notifyDataSetChanged();
            }
        });
        childViewHolder.tvName.setText(couponData.get(childPosition).getCouponsName());
        if(couponData.get(childPosition).getMinimum()==0){
            childViewHolder.tvUsedCase.setText("无门槛");
        }else {
            childViewHolder.tvUsedCase.setText("满" + String.format("%.2f",couponData.get(childPosition).getMinimum()) + "元使用");
        }

        childViewHolder.tvUsedDescription.setText(couponData.get(childPosition).getDescription());
        long endTime =  couponData.get(childPosition).getEndTime();
        long startTime =  couponData.get(childPosition).getCreateTime();
        if (endTime == 0) {
            childViewHolder.tvTime.setText("永久有效");
        } else {
            childViewHolder.tvTime.setText(TimeUtils.formatTimeDifference4(startTime)+"-"+TimeUtils.formatTimeDifference4(endTime));
        }


        childViewHolder.tvUsedMoney.setText(String.format("%.2f",couponData.get(childPosition).getDenomination())+"");
        if(couponData.get(childPosition).getUseType().equals("only")){
            childViewHolder.imgCouponsType.setImageResource(R.mipmap.bg_coupons_unusered_red);
        }else if(couponData.get(childPosition).getUseType().equals("all")){
            childViewHolder.imgCouponsType.setImageResource(R.mipmap.bg_coupons_common_yellow);
        }else {
            childViewHolder.imgCouponsType.setVisibility(View.GONE);
        }

        if(couponData.get(childPosition).getStoreId()!=0){ //黄色门店
            childViewHolder.ll_bg.setBackgroundResource(R.mipmap.coupons_red_bg);
            childViewHolder.ll_xuxian.setBackgroundResource(R.mipmap.coupons_xuxian_red_bg);
            childViewHolder.tv_use_store.setBackgroundResource(R.drawable.selector_user_store_bg);
            childViewHolder.tv_use_store.setTextColor(yellowColor);
            childViewHolder.tv_use_store.setText("限"+ UserHelper.getInstance().getOwnerInfoModel(mContext).getStoreName()+"使用");
            childViewHolder.tvName.setTextColor(yellowColor);
            childViewHolder.tvUsedData.setTextColor(yellowColor);
            childViewHolder.tvTime.setTextColor(yellowColor);
            childViewHolder.tvUsedMoney.setTextColor(yellowColor);
            childViewHolder.tvUsedYuan.setTextColor(yellowColor);
            childViewHolder.cbPicked.setBackground(mContext.getResources().getDrawable(R.drawable.selector_yellow_checkbox));
        }else { // 0 系统
            childViewHolder.ll_bg.setBackgroundResource(R.mipmap.coupons_blue_bg);
            childViewHolder.ll_xuxian.setBackgroundResource(R.mipmap.coupons_xuxian_blue_bg);
            childViewHolder.tv_use_store.setBackgroundResource(R.drawable.selector_user_store_system_coupons_bg);
            childViewHolder.tv_use_store.setTextColor(themeColor);
            childViewHolder.tv_use_store.setText("限平台商城使用");
            childViewHolder.tvName.setTextColor(themeColor);
            childViewHolder.tvUsedData.setTextColor(themeColor);
            childViewHolder.tvTime.setTextColor(themeColor);
            childViewHolder.tvUsedMoney.setTextColor(themeColor);
            childViewHolder.tvUsedYuan.setTextColor(themeColor);
            childViewHolder.cbPicked.setBackground(mContext.getResources().getDrawable(R.drawable.selector_paid_checkbox));
        }


//        if (couponData.get(childPosition).getMinimum() > sumGoodsPrice) {
//            childViewHolder.tvName.setTextColor(disenbledColor);
            // childViewHolder.llTimeWrapper.setBackgroundColor(disenbledColor);
//        } else {
//            childViewHolder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.appThemeColor));
            //childViewHolder.llTimeWrapper.setBackgroundColor(ContextCompat.getColor(mContext, R.color.appThemeColor));
//        }

        return convertView;
    }



    public GoodsCouponEntity getPickedGoodsCoupon() {
        return pickedGoodsCoupon;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }


    class ChildViewHolder {
        LinearLayout ll_bg;
        LinearLayout ll_xuxian;
        TextView tv_use_store;
        CheckBox cbPicked;
        TextView tvName;
        ImageView imgCouponsType;
        TextView tvUsedCase;
        TextView tvUsedData;
        TextView tvTime;
        TextView tvUsedMoney;
        TextView tvUsedYuan;
        TextView tvUsedDescription;
    }
}
